home *** CD-ROM | disk | FTP | other *** search
- /*
- File: TBitmap.cp
-
- Contains: A simple bitmap class
-
- Written by: Arno Gourdol
-
- Copyright: © 1994-1995 by Apple Computer, Inc., all rights reserved.
-
-
- */
-
- #include "TBitmap.h"
-
- #include <Resources.h>
-
-
-
- // --------------------------------------------------------------------
- // TBitmap
- // --------------------------------------------------------------------
- // Constructor, from a PICT resource, builds a bitmap of the
- // specified depth
-
- TBitmap::TBitmap(SInt16 pictureID, UInt16 depth) :
- fGWorld(NULL)
- {
- PicHandle picture = (PicHandle)::Get1Resource('PICT', pictureID);
-
- if (picture != NULL)
- {
- // Geet the size of the picturee
- CRect frame((**picture).picFrame);
-
- fBounds = frame;
-
- // Create a new offscreen world of the specified dimensions
- OSErr err = ::NewGWorld(&fGWorld, depth, frame, NULL, NULL, 0);
- assert(err == memFullErr || err == noErr);
-
- if (fGWorld != NULL)
- {
- fPort = (GrafPtr)fGWorld;
- fDepth = depth;
- fIsColor = (fDepth > 1);
- }
- else
- {
- fPort = NULL;
- fDepth = 1;
- fIsColor = false;
- }
-
- // If the offscreen was created successfully,
- // draw the picture inside it
- if (Lock())
- {
- ::DrawPicture(picture, frame);
- Unlock();
- }
-
-
- ::ReleaseResource((Handle)picture);
- }
- }
-
-
-
- // --------------------------------------------------------------------
- // TBitmap
- // --------------------------------------------------------------------
- // Constructor. Creates a bitmap of the specified dimensions and depth
-
- TBitmap::TBitmap(CRect bounds, UInt16 depth) :
- fBounds(bounds),
- fDepth(depth),
- fGWorld(NULL)
- {
- fGWorld = NULL;
-
- CRect rect(fBounds);
- // If the requested depth is 0...
- if (depth == 0)
- {
- // ... convert the dimension of the bitmap to screen
- // coordinates. This will cause NewGWorld to use the deepest
- // depth available on this area of the screen
- TDrawContext drawContext;
- drawContext.ConvertToScreen(rect);
- }
-
- OSErr err = ::NewGWorld(&fGWorld, depth, rect, NULL, NULL, 0);
- assert(err == memFullErr || err == noErr);
-
- if (fGWorld != NULL)
- {
- fPort = (GrafPtr)fGWorld;
- fDepth = depth;
- fIsColor = (fDepth > 1);
- }
- else
- {
- fPort = NULL;
- fDepth = 1;
- fIsColor = false;
- }
- }
-
-
-
- // --------------------------------------------------------------------
- // ~TBitmap
- // --------------------------------------------------------------------
-
- TBitmap::~TBitmap()
- {
- if (fGWorld != NULL)
- {
- ::DisposeGWorld(fGWorld);
- }
- }
-
-
-
- // --------------------------------------------------------------------
- // SetBits
- // --------------------------------------------------------------------
-
- void TBitmap::SetBits(const void* data, UInt32 length, UInt32 offset, UInt16 depth)
- {
- #pragma unused(data, length, offset, depth)
-
- }
-
-
-
- // --------------------------------------------------------------------
- // Bits
- // --------------------------------------------------------------------
-
- void* TBitmap::Bits(void) const
- {
- assert(fLockCount > 0);
-
- if (fGWorld != NULL)
- {
- return ::GetPixBaseAddr(::GetGWorldPixMap(fGWorld));
- }
- else
- {
- return NULL;
- }
- }
-
-
-
- // --------------------------------------------------------------------
- // BitsLength
- // --------------------------------------------------------------------
-
- long TBitmap::BitsLength(void) const
- {
- return 0;
- }
-
-
-
- // --------------------------------------------------------------------
- // BytesPerRow
- // --------------------------------------------------------------------
-
- long TBitmap::BytesPerRow(void) const
- {
- if (fGWorld != NULL)
- {
- return (**GetGWorldPixMap(fGWorld)).rowBytes;
- }
- else
- {
- return 0;
- }
- }
-
-
-
- // --------------------------------------------------------------------
- // ColorSpace
- // --------------------------------------------------------------------
-
- UInt16 TBitmap::ColorSpace(void) const
- {
- return fDepth;
- }
-
-
-
- // --------------------------------------------------------------------
- // Bounds
- // --------------------------------------------------------------------
-
- CRect TBitmap::Bounds(void) const
- {
- return fBounds;
- }
-
-
-
- // --------------------------------------------------------------------
- // Lock
- // --------------------------------------------------------------------
- // Prepare the Bitmap for drawing. Retruns true if the bitmap is
- // ready and drawing can proceed
-
- Boolean TBitmap::Lock(void)
- {
- Boolean result = false;
-
- if (fGWorld != NULL && ::LockPixels(::GetGWorldPixMap(fGWorld)))
- {
- ::GetGWorld((CGrafPtr*)&fSavePort, &fSaveGDevice);
- result = TDrawContext::Lock();
- if (result)
- {
- ::SetGWorld(fGWorld, NULL);
- }
- else
- {
- ::UnlockPixels(::GetGWorldPixMap(fGWorld));
- }
- }
-
- return result;
- }
-
-
-
- // --------------------------------------------------------------------
- // Unlock
- // --------------------------------------------------------------------
- // Call when the drawing in the bitmap is done. Only call if Lock()
- // returned true.
- // Restores the drawing environmnt
-
- void TBitmap::Unlock(void)
- {
- assert(fGWorld != NULL);
-
- ::UnlockPixels(::GetGWorldPixMap(fGWorld));
- ::SetGWorld((CGrafPtr)fSavePort, fSaveGDevice);
-
- TDrawContext::Unlock();
- }
-
-
-
-